]> git.r.bdr.sh - rbdr/map/blob - Map/Presentation/Base Components/MapRender/MapBlockers.swift
Address the lint warnings
[rbdr/map] / Map / Presentation / Base Components / MapRender / MapBlockers.swift
1 // Copyright (C) 2024 Rubén Beltrán del Río
2
3 // This program is free software: you can redistribute it and/or modify
4 // it under the terms of the GNU General Public License as published by
5 // the Free Software Foundation, either version 3 of the License, or
6 // (at your option) any later version.
7
8 // This program is distributed in the hope that it will be useful,
9 // but WITHOUT ANY WARRANTY; without even the implied warranty of
10 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 // GNU General Public License for more details.
12
13 // You should have received a copy of the GNU General Public License
14 // along with this program. If not, see https://map.tranquil.systems.
15 import SwiftUI
16
17 struct MapBlockers: View {
18
19 let mapSize: CGSize
20 let vertexSize: CGSize
21 let blockers: [Blocker]
22
23 let cornerSize = CGSize(width: 2.0, height: 2.0)
24
25 var body: some View {
26 ForEach(blockers, id: \.id) { vertex in
27 Path { path in
28 path.addRoundedRect(
29 in: CGRect(
30 origin: CGPoint(
31 x: w(vertex.position.x) + 3 * vertexSize.width,
32 y: h(vertex.position.y) - vertexSize.height * 2 / 3),
33 size: CGSize(width: vertexSize.width / 2, height: vertexSize.height * 2)
34 ), cornerSize: cornerSize)
35 }.fill(Color.Map.blockerColor)
36 }
37 }
38
39 func h(_ dimension: CGFloat) -> CGFloat {
40 max(0.0, min(mapSize.height, dimension * mapSize.height / 100.0))
41 }
42
43 func w(_ dimension: CGFloat) -> CGFloat {
44 max(0.0, min(mapSize.width, dimension * mapSize.width / 100.0))
45 }
46 }
47
48 #Preview {
49 MapBlockers(
50 mapSize: CGSize(width: 400.0, height: 400.0), vertexSize: CGSize(width: 25.0, height: 25.0),
51 blockers: [
52 Blocker(id: 0, position: CGPoint(x: 50.0, y: 50.0)),
53 Blocker(id: 1, position: CGPoint(x: 10.0, y: 20.0)),
54 ])
55 }